$(function(){
	var parentHeight,parentWidth;
	parentHeight = $("#project-box").height() - 183;
    parentWidth = $("#project-box").width();
	$("#project-table").jqGrid({
		url: contextPath + "/Project/queryProjectJqg",
		datatype: "json",
		height: parentHeight,
	    width: parentWidth,
	    autowidth: true,
		colNames:['','项目名称','','产品线名称','项目规模','项目合同金额(元)','项目属性','项目经理','项目开始时间','项目结束时间','初验时间','终验时间','回款金额(元)','使用合作方','备注信息','操作'],
		colModel:[
			{name:'id',index:'id',align:"center",hidden:true},
			{name:'name',index:'name',align:"center"},
			{name:'productLineId',index:'productLineId',align:"center",hidden:true},
			{name:'productLineName',index:'productLineName',align:"center"},
			{name:'size',index:'size',align:"center"},
			{name:'money',index:'money',align:"center"},
			{name:'property',index:'property',align:"center"},
			{name:'manager',index:'manager',align:"center"},
			{name:'beginTime',index:'beginTime',align:"center"},
			{name:'endTime',index:'endTime',align:"center"},
			{name:'firstCheckTime',index:'firstCheckTime',align:"center"},
			{name:'lastCheckTime',index:'lastCheckTime',align:"center"},
			{name:'payment',index:'payment',align:"center"},
			{name:'cooperator',index:'cooperator',align:"center"},
			{name:'note',index:'note',align:"center"},
			{name:'operate',formatter: operation,"sortable":false,"width":160}
		],
		sortname:'id',
		sortorder:'asc',
		viewrecords: true,
		rowNum: 20,
		rowList:[5,10,20],
		pager: '#project-pager'
	});
	$("#project-table").jqGrid('navGrid', '#project-pager', {edit : false,add : false,del : false,search: false});
	// 查询产品线
	$.ajax({
        url: contextPath + "/ProductLine/queryAllProductLine",
        type: "get",
        cache: false,
        dataType: "json",
        success: function(data){
            var str = '<option value="">全部</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#productLineId").html(str);
        },
        error: function(data){
            console.log(data);
            $("#productLineId").html('<option value="">数据异常</option>');
        }
    });
	// 查询按钮
    $("#query").on("click",function(){
    	var _id = $('#productLineId').val()?Number($('#productLineId').val()):null;
	    $("#project-table").setGridParam({
	        url : contextPath + "/Project/queryProjectJqg",
	        postData : {
	        	queryCond:{
	        		'productLineId': _id,
	        		'name': $('#name').val()
	        	}
	        },
            page:1
	    }).trigger("reloadGrid");
    });
    // 导入
	// $('#fileupload').fileupload({
	// 	dataType: 'json',
	// 	done: function (e, data) {
	// 		if(data.result.code == "1") {
	// 			toastr.success('导入项目成功！');
	// 		} else if(data.result.code == "2") {
	// 			toastr.error(data.result.message,'操作失败');
	// 		} else {
	// 			toastr.success('导入项目部分成功！');
	// 		}
	// 		 $("#project-table").trigger("reloadGrid");
	// 	}
	// });
	// 导出
	$("#exportProjectList").on('click',function(){
		var url = "Project/exportProjectList";
		window.open(url);
    });
});
function operation(cellvalue,options,rowObject){
	var str = "";
	// if([[${#authorization.expression('hasAuthority(''modifyAuthority'')')}]]){
		str += "<input class='btn btn-primary btn-xs modify' type='button' value='修改'>";
	// }
	// if([[${#authorization.expression('hasAuthority(''deleteAuthority'')')}]]){
		str += "&nbsp;<input class='btn btn-danger btn-xs del' type='button' value='删除' data-toggle='modal' data-target='#deleteNotice'>";
	// }
    return str;
}
//修改
var projectData = null;
/* 修改 || 删除 */
$("#project-table").on('click','.modify,.del',function(){
	var id = $(this).parent().parent().attr('id');
	projectData = $("#project-table").jqGrid('getRowData',id);
    delete projectData.operate;
    if($(this).hasClass("modify")){
    	// $.ajax({
	    //     url:contextPath + '/Project/queryProjectById/'+projectData.id,
	    //     type:"get",
	    //     contentType:"application/json",
	    //     dataType:"json",
	    //     success:function(data){
        //
         //        projectData.constructionOrganizationString = "";
	    //     	for(var i = 0,len = data.constructionOrganization.length;i < len;i++){
			// 		projectData.constructionOrganizationString += data.constructionOrganization[i].name + ";";
	    //     	}
         //        projectData.constructionOrganization = data.constructionOrganization;
	            window.location.hash = "qualitycontrol-projectGroup-modifyProject";
	    //     },
	    //     error:function(){
	    //         toastr.error('操作失败，请稍后再试！');
	    //         console.log(data);
	    //     }
	    // });
    }else{
    	projectData.productLineId = Number(projectData.productLineId);
    	projectData.id = Number(projectData.id);
    }
});
$("#deleteData").on('click',function(){
	$.ajax({
        url:contextPath + '/Project/deleteProject',
        type:"delete",
        contentType:"application/json",
        dataType:"json",
        data: JSON.stringify(projectData),
        success:function(data){
            if(data.code == 1){
                $('#deleteNotice').modal('hide');
                toastr.success('删除成功！');
                var _id = $('#productLineId').val()?Number($('#productLineId').val()):null;
                $("#project-table").setGridParam({
                    url : contextPath + "/Project/queryProjectJqg",
                    postData : {
                        queryCond:{
                            'productLineId': _id,
                            'name': $('#name').val()
                        }
                    },
                    page:1
                }).trigger("reloadGrid");
                projectData = null;
                //to
            }else{
                toastr.error(data.message,'删除失败！');
            }
        },
        error:function(){
            toastr.error('操作失败，请稍后再试！');
            console.log(data);
        }
    });
});
$(window).resize(function(){
	$("#project-table").setGridWidth($('#project-box').width());
	$("#project-table").setGridHeight($('#project-box').height() - 183);
});
// 侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click',function() {
    var time = 0;
    var set = setInterval(function (){
        $("#project-table").setGridWidth($('#project-box').width());　　
        $("#project-table").setGridHeight($('#project-box').height() - 183);
        time++;
        if(time >= 200){
            clearInterval(set);
        }
    },time);
});
$("#exportProject").on('click',function(){
	var url = "user/exportSubsystemUserList";
	window.open(url);
});